group
$group
Separates documents into groups according to a group key you define and outputs one document for each unique group key. You can also use accumalator operators to compute data for each group.
A group key can be an existing field, a group of fields, or the result of an expression. Use the _id field in the $group pipeline stage to set the group key.
Note: The
$groupmethod does not order its output documents - instead, you can sort documents with a$sortstage earlier in the pipeline.
Syntax
{
"$group":
{
"_id": "<expression>", // Group key
"<customField1>": { "<accumulator1>" : "<expression1>" },
}
}
| Field | Type | Description | Required |
|---|---|---|---|
_id | expression | Define an expression for the group key. | Required |
| Custom field | Define a custom field, then define an accumulator operator and expression pair to compute the data you want for the custom field. You can define multiple custom fields. | Optional |
Accumulator operators
| Accumulator operator | Returns | Notes |
|---|---|---|
$addToSet | An array of unique expression values for each group | Order of the array elements is undefined |
$avg | An average of numerical values | Ignores non-numeric values |
$count | The number of documents in a group | |
$first | A value from the first document for each group | For order, sort documents with a $sort stage earlier in the pipeline. |
$last | A value from the last document for each group | For order, sort documents with a $sort stage earlier in the pipeline. |
$max | The highest expression value for each group | |
$min | The lowest expression value for each group | |
$push | An array of expression values for documents in each group | |
$stdDevPop | The population standard deviation of the input values | |
$sum | A sum of numerical values | Ignores non-numeric values |
Example
In the following example, in the $group stage of the pipeline, the group key is defined by telemetry sensor source id. The custom fields are defined as follows:
"count": Counts the documents for each group"avgtemp": Computes the average of thetempvalues for each group"low": Gets the temp value of the first document in the group using the$firstoperator. The documents are sorted bytempin ascending order in a previous stage."high": Gets the temp value of the last document in the group using the$lastoperator. The documents are sorted bytempin ascending order in a previous stage.
Sample Request
[
{
"$match": {
"temp": {
"$exists": true
},
"_tsMetadata._sourceId": {
"$exists": true
}
}
},
{
"$sort": {
"temp": 1
}
},
{
"$group": {
"_id": "$_tsMetadata._sourceId",
"count": {
"$count": {}
},
"avgtemp": {
"$avg": "$temp"
},
"low": {
"$first": "$temp"
},
"high": {
"$last": "$temp"
}
}
}
]
Sample Response
{
"_list": [
{
"high": 63,
"low": 63,
"count": 1,
"_id": "b1da8f7b-d6fd-46e0-a7f1-69d2118c05c2",
"avgtemp": 63
},
{
"high": 65,
"low": 38,
"count": 3,
"_id": "7d4a1c16-c2e4-4159-a5b8-5814f4bfe6e2",
"avgtemp": 55.333333333333336
},
{
"high": 83,
"low": 43,
"count": 3,
"_id": "ef4b5a5d-a5dd-48b0-b9c1-3618552cfe09",
"avgtemp": 61.333333333333336
},
{
"high": 85,
"low": 52,
"count": 3,
"_id": "ed11cc38-97c8-486e-b21e-2cccb7271173",
"avgtemp": 72.33333333333333
}
]
}